package 将x减到0的最小操作数;

class Solution {
    public int minOperations(int[] nums, int x) {
        int sum = 0;
        int n = nums.length;
        for (int num : nums) {
            sum += num;
        }
        int target = sum - x;
        int left = 0;
        int right = 0;
        int count = 0;
        int ret = Integer.MAX_VALUE;
        while (right < n){
            count += nums[right];
            while (left <= right && count > target){
                count -= nums[left++];
            }
            if (count == target){
                ret = Math.min(ret,left + n - right - 1);
            }
            right++;
        }
        return ret == Integer.MAX_VALUE ? -1 : ret;
    }
}